// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package com.example.basic.utils;

import android.content.Context;
import android.content.ContextWrapper;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.res.AssetManager;
import android.os.Process;
import android.preference.PreferenceManager;

import androidx.annotation.VisibleForTesting;

public class ApplicationContextUtils {
    private static final String TAG = "ContextUtils";
    private static Context sApplicationContext;
    private static String sProcessName;

    private static boolean isDebugPackage = false;

    public static void init(Context context) {
        isDebugPackage = isApkInDebug(context);
    }

    //判断当前应用是否是debug状态
    private static boolean isApkInDebug(Context context) {
        try {
            ApplicationInfo info = context.getApplicationInfo();
            return (info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
        } catch (Exception e) {
            return false;
        }
    }

    private static class Holder {
        private static SharedPreferences sSharedPreferences = fetchAppSharedPreferences();
    }

    public static Context getApplicationContext() {
        return sApplicationContext;
    }

    public static void initApplicationContext(Context appContext) {
        if (sApplicationContext != null && sApplicationContext != appContext) {
            throw new RuntimeException("Attempting to set multiple global application contexts.");
        }
        initJavaSideApplicationContext(appContext);
    }

    private static SharedPreferences fetchAppSharedPreferences() {
        return PreferenceManager.getDefaultSharedPreferences(sApplicationContext);
    }

    public static SharedPreferences getAppSharedPreferences() {
        return Holder.sSharedPreferences;
    }

    @VisibleForTesting
    public static void initApplicationContextForTests(Context appContext) {
        initJavaSideApplicationContext(appContext);
        Holder.sSharedPreferences = fetchAppSharedPreferences();
    }

    private static void initJavaSideApplicationContext(Context appContext) {
        if (appContext == null) {
            throw new RuntimeException("Global application context cannot be set to null.");
        }
        sApplicationContext = appContext;
    }

    public static AssetManager getApplicationAssets() {
        Context context = getApplicationContext();
        while (context instanceof ContextWrapper) {
            context = ((ContextWrapper) context).getBaseContext();
        }
        return context.getAssets();
    }

    public static boolean isIsolatedProcess() {
        try {
            return (Boolean) Process.class.getMethod("isIsolated").invoke(null);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static String getProcessName() {
        if (sProcessName != null) {
            return sProcessName;
        }
        try {
            Class<?> activityThreadClazz = Class.forName("android.app.ActivityThread");
            Object activityThread =
                    activityThreadClazz.getMethod("currentActivityThread").invoke(null);
            sProcessName =
                    (String) activityThreadClazz.getMethod("getProcessName").invoke(activityThread);
            return sProcessName;
        } catch (Exception e) { // No multi-catch below API level 19 for reflection exceptions.
            throw new RuntimeException(e);
        }
    }

    public static void applyNewTheme(int themeId) {
        sApplicationContext.setTheme(themeId);
    }
}
